package com.factory.iamp.controller;

import com.factory.iamp.pojo.User;
import com.factory.iamp.service.QuestionService;
import com.factory.iamp.utils.helper.FileUploadHelper;
import com.factory.iamp.utils.helper.PageHelper;
import com.factory.iamp.utils.token.LoginToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/question")
@Validated
public class QuestionController {
    @Resource
    QuestionService questionService;

    @GetMapping("/find/{limit}/{page}")
    @LoginToken(checkPrivate = true)
    public PageHelper findQuestionRecord(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "department", defaultValue = "", required = false) String department,
            @RequestParam(value = "product_type", defaultValue = "", required = false) String product_type,
            @RequestParam(value = "description", defaultValue = "", required = false) String description
    ) {

        String QueryStr = "1";
        if(department.length() > 0) QueryStr += " and department like \"%" + department + "%\"";
        if(product_type.length() > 0) QueryStr += " and product_type like \"%" + product_type + "%\"";
        if(description.length() > 0) QueryStr += " and question_description like \"%" + description + "%\"";
        PageHelper result = new PageHelper(questionService.QueryCount(QueryStr), page, limit);
        result.setData(questionService.QueryListPaged(result, QueryStr));
        return result;
    }

    @PostMapping("/add")
    @LoginToken(checkPrivate = true)
    public void addQuestionRecord(
            @RequestParam("name") String name,
            @RequestParam("department") String department,
            @RequestParam("product_type") String product_type,
            @RequestParam("process_type") String process_type,
            @RequestParam("summary") String summary,
            @RequestParam("description") String description,
            @RequestParam("method") String method,
            @RequestParam(value = "recorder", defaultValue = "", required = false) String recorder,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestAttribute("iampUser") User user
    ) throws Throwable {
        if(recorder.equals("")) recorder = user.getUid();
        if(file == null) {
            questionService.addQuestionRecord(
                    name, department, product_type, process_type, summary, description, method, recorder, ""
            );
        }else{
            questionService.addQuestionRecord(
                    name, department, product_type, process_type, summary, description, method, recorder,
                    FileUploadHelper.uploadedPath("/data/question_pic/" + user.getUid() + "/", file)
            );
        }
    }

    @PostMapping("/alter/{qid}")
    @LoginToken(checkPrivate = true)
    public void alterQuestionRecord(
            @PathVariable("qid") int qid,
            @RequestParam("name") String name,
            @RequestParam("department") String department,
            @RequestParam("product_type") String product_type,
            @RequestParam("process_type") String process_type,
            @RequestParam("summary") String summary,
            @RequestParam("description") String description,
            @RequestParam("method") String method,
            @RequestParam(value = "recorder", defaultValue = "", required = false) String recorder,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestAttribute("iampUser") User user
    ) throws Throwable {
        if(recorder.equals("")) recorder = user.getUid();
        if(file == null){
            questionService.alterQuestionRecord(
                    qid, name, department, product_type, process_type, summary, description, method, recorder, ""
            );
        }else{
            questionService.alterQuestionRecord(
                    qid, name, department, product_type, process_type, summary, description, method, recorder,
                    FileUploadHelper.uploadedPath("/data/question_pic/" + user.getUid() + "/", file)
            );
        }
    }

    @DeleteMapping("/delete/{qid}")
    @LoginToken(checkPrivate = true)
    public void deleteQuestionRecord(@PathVariable("qid") int qid) {
        questionService.deleteQuestionRecord(qid);
    }
}
